#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main()
{
    char cmd[64];
    printf("Enter command: ");
    fflush(stdout);

    if(fgets(cmd, sizeof(cmd), stdin) == NULL)
    {
        perror("fgets failed");
        return 1;
    }

    size_t len = strlen(cmd);
    if(len > 0 && cmd[len - 1] == '\n')
    {
        cmd[len - 1] = '\0';
    }

    char cmd_copy[64];
    strncpy(cmd_copy, cmd, sizeof(cmd_copy));

    char *token = strtok(cmd_copy, " ");
    if(token && strcmp(token, "cd") == 0)
    {
        char *path = strtok(NULL, " ");
        if(!path)
        {
            fprintf(stderr, "cd: missing operand\n");
            return 1;
        }

        if(chdir(path) != 0)
        {
            perror("cd failed");
            return 1;
        }

        char cwd[128];
        if(getcwd(cwd, sizeof(cwd)) != NULL)
        {
             printf("Current directory: %s\n", cwd);
        }
        else
        {
            perror("getcwd failed");
        }
    }
    else
    {
        printf("Not a cd command\n");
    }

    return 0;
}   
